<template>
  <div class="son">
    <h1>You are my son</h1>
    <p>我的资产{{ mymoney }}W元,豪车{{ soncar }}辆</p>
    <p>我有女朋友了,名字叫:{{ hername }}</p>
    <button @click="tellname">告诉父母</button>
    <p>我兄弟找了个富婆,名字:{{ bgfname }}</p>
  </div>
</template>

<script>
//引入
import eventBus from "@/utils/myeventbus.js";
export default {
  //接收父组件传过来的参数绝对不能在data里定义,data是函数
  //props:用于接收父组件所传递到的数据
  //1.0定义为数组,里面就是一个个的字符串变量名.
  // props: ["mymoney", "soncar"],
  //2.0定义为对象
  props: {
    mymoney: {
      type: [Number, String],
      required: true,
      default: 0,
    },
    soncar: {
      type: [Number, String],
      required: true,
      default: 0,
    },
  },
  data() {
    return {
      hername: "小红花",
      bgfname: "?",
    };
  },
  methods: {
    //子传父
    tellname() {
      //$emit(自定义事件类型,传递的数据)
      //通过this发出的事件,只能在父组件进行监听并响应
      this.$emit("getname", { name: this.hername, gender: "女" });
    },
  },
  // 兄传递
  mounted() {
    eventBus.$on("brother_getbgfname", (data) => {
      console.log(this);
      console.log(data);
      this.bgfname = data;
    });
  },
};
</script>

<style lang="less" scoped>
.son {
  width: 100%;
  background-color: cyan;
  padding: 10px;
}
</style>